﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using TimKen.DataEntity;
using TimKen.BussLogic;
using System.Globalization;
using System.Data;
using TimKen.DataAccess;

namespace TimKen.Web
{
    public partial class TimKenSite : System.Web.UI.MasterPage
    {
        public DataRowCollection drcHelpClass;
        public string ProductStr = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string path = Request.Url.PathAndQuery.ToString(); //Request.RawUrl;
                if (path != "/User/Login.aspx?language=en-US" && path != "/User/Login.aspx?language=zh-CN")
                    Session.Add("pathUrl", path);
                if (Session["lan"] != null)
                    SetLanguage(Session["lan"].ToString());
                //Session.Add("lan", language);   
            }
            ProductStr = new TimKen.App.WebPage().GetProductList();
            NewHelp();
        }
        /// <summary>
        /// 绑定帮助分类和帮助
        /// </summary>
        private void NewHelp()
        {

            //绑定帮助分类和帮助
            string sql = "select *  FROM   HelpClass Order By ClassID";

            DataSet ds = DbHelperSQL.Query(sql);

            drcHelpClass = ds.Tables[0].Rows;
        }

        public DataRowCollection GetHelpByClass(int classid)
        {
            ArticleManager am = new ArticleManager();
            DataSet ds = am.GetList(" TypeID=4 and LanguageID=" + this.LanguageID + " AND ClassID=" + classid);
            return ds.Tables[0].Rows;
        }
        protected void SetLanguage(string language)
        {
            HttpCookie cookie = new HttpCookie("Language");
            DateTime dt = DateTime.Now;//定义时间对象
            TimeSpan ts = new TimeSpan(0, 20, 0);
            cookie.Expires.Add(ts);
            cookie.Value = language;
            HttpContext.Current.Response.AppendCookie(cookie);
            CultureInfo culture = new CultureInfo(language);
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
        }
        #region 常用
        public string CurrentUserName
        {
            get { return Session["User"] == null ? null : ((UserS)Session["User"]).UserName; }
        }

        public UserS CurrentUser
        {
            get
            {
                if (
                    Session["User"] != null)
                {
                    UserS u = Session["User"] as UserS;
                    return u;
                }
                return null;
            }
        }

        public string GetLangString(string key)
        {
            return TimKen.Localization.LocalizationUtility.GetText(key);
        }

        public int LanguageID
        {
            get
            {
                switch (Thread.CurrentThread.CurrentUICulture.ToString())
                {
                    case "zh-CN": return 1;
                    case "en-US": return 2;
                    default: return 1;
                }
            }
        }

        /// <summary>
        /// 在前台通过js弹出消息窗口
        /// </summary>
        /// <param name="msg">弹出消息</param>
        protected void Alert(string msg)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "<script>alert(\"" + msg + "\");</script>");
        }
        #endregion
    }
}